package com.eonmind.ai.mapper;

import com.eonmind.common.contract.data.base.BaseMongoMapper;
import com.eonmind.common.contract.enums.DelEnum;
import com.eonmind.common.provider.entity.ai.AiConversationMessage;
import com.mongoplus.aggregate.AggregateWrapper;
import com.mongoplus.conditions.query.QueryWrapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AiMessageMapper extends BaseMongoMapper<AiConversationMessage> {


    public List<AiConversationMessage> selectByConversationId(Long conversationId) {
        QueryWrapper<AiConversationMessage> eq = createMgoQuery()
                .eq(AiConversationMessage::getConversationId, conversationId)
                .eq(AiConversationMessage::getDel, DelEnum.UN_DEL.getCode());
        return list(eq);
    }

    public List<AiConversationMessage> selectByConversationIdAndLimit(Long conversationId, int lastN) {
        QueryWrapper<AiConversationMessage> eq = createMgoQuery().eq(AiConversationMessage::getConversationId, conversationId)
                .orderByDesc(AiConversationMessage::getCreateTime)
                .eq(AiConversationMessage::getDel, DelEnum.UN_DEL.getCode());
        AggregateWrapper limit = new AggregateWrapper().match(eq).limit(lastN);
        return list(limit);
    }
}
